﻿using System;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using SAPPDI.Entitys;
using TestImpresion.Clases;
using TestImpresion.Clases.Listas;

namespace TestImpresion.Controles
{
    public partial class Insertos : UserControl
    {

        private readonly DependencyProperty _datasourceProperty =
        DependencyProperty.Register("Datasource", typeof(InsertosFormulario), typeof(Insertos), new PropertyMetadata(new InsertosFormulario()));

        [System.ComponentModel.Category("User properties")]
        [System.ComponentModel.Description("User Datasource")]
        public InsertosFormulario Datasource
        {
            get { return (InsertosFormulario)GetValue(_datasourceProperty); }
            set { SetValue(_datasourceProperty, value); }
        }        

        public Insertos()
        {
            InitializeComponent(); 

            this.LayoutRoot.DataContext = this.Datasource;
            //this.ComprarPor.Datasource= new ComparPorFormulario();
        }

        private void TxtNoInsertos_OnTextChanged(object sender, TextChangedEventArgs e)
        {
            var textBox = (TextBox)sender;
            int noInsertos;
            if (!Int32.TryParse(textBox.Text, out noInsertos)) return;

            this.DinInsertos.SetNumChildren(noInsertos);
            var datos = new DatosCombos();
            var tipopapel = datos.ObtenTiposPapel();
            if (noInsertos < this.Datasource.ListaInsertos.Count)
            {
                var nuevaLista = new ObservableCollection<SAPPDI.Entitys.Insertos>();
                var nuevaListaComprar = new ObservableCollection<ComparPorFormulario>();
                for (var i = 0; i < noInsertos; i++)
                {
                    nuevaLista.Add(new SAPPDI.Entitys.Insertos(this.Datasource.ListaInsertos[i]){ NumeroInsertos = noInsertos});
                    nuevaListaComprar.Add(new ComparPorFormulario(this.Datasource.ListaComprarPor[i]));
                }
                this.Datasource.ListaInsertos = nuevaLista;
                this.Datasource.ListaComprarPor = nuevaListaComprar;
            }

            foreach (var inserto in this.Datasource.ListaInsertos)
            {
                inserto.NumeroInsertos = noInsertos;
            }

            for (int i = this.Datasource.ListaInsertos.Count; i < noInsertos; i++)
            {
                this.Datasource.ListaInsertos.Add(new SAPPDI.Entitys.Insertos { 
                    TipoPapels = tipopapel, 
                    NumeroInsertos = noInsertos,
                    ListaEntrePliegos = datos.GetEntreQuePliegos()
                });
                this.Datasource.ListaComprarPor.Add(new ComparPorFormulario());
            }
        }

        private void DinInsertos_OnSelectedEvent(object sender, InsertosEventArgs e)
        {
            if (e.IndexAnterior == e.IndexSelected)
            {
                var datos = new DatosCombos();
                this.Datasource.ListaInsertos[e.IndexAnterior].ColocaNuevosValores(this.Datasource.CurrentInsertos);
                this.Datasource.ListaComprarPor[e.IndexAnterior] = this.Datasource.ComprarInsertosFormulario;
                this.cmbTipoPapel.SelectedIndex = -1;
                this.Datasource.CurrentInsertos.ColocaNuevosValores( new SAPPDI.Entitys.Insertos
                    {
                        TipoPapels = datos.ObtenTiposPapel(),
                        ListaEntrePliegos = datos.GetEntreQuePliegos(),
                        NumeroInsertos = this.Datasource.ListaInsertos[e.IndexAnterior].NumeroInsertos
                    });
                this.Datasource.ComprarInsertosFormulario= new ComparPorFormulario();
                this.ComprarPor.SetCostos(this.Datasource.ComprarInsertosFormulario);
                this.lblTituloInserto.Content = string.Empty;
            }
            else
            {
                if (e.IndexAnterior >= 0)
                {
                    this.Datasource.ListaInsertos[e.IndexAnterior].ColocaNuevosValores(this.Datasource.CurrentInsertos);
                    this.Datasource.ListaComprarPor[e.IndexAnterior] = this.Datasource.ComprarInsertosFormulario;
                }
                    this.cmbTipoPapel.SelectedIndex = -1;
                
                this.Datasource.CurrentInsertos.ColocaNuevosValores( this.Datasource.ListaInsertos[e.IndexSelected]);
                this.Datasource.ComprarInsertosFormulario = this.Datasource.ListaComprarPor[e.IndexSelected];
                this.ComprarPor.SetCostos(this.Datasource.ComprarInsertosFormulario);
                this.lblTituloInserto.Content = "Inserto " + (e.IndexSelected + 1);
            }
        }     

        private void ButtonCerrar_Click(object sender, RoutedEventArgs e)
        {
            Popup1.IsOpen = false;
        }

        private void BtnRevisar_OnClick(object sender, RoutedEventArgs e)
        {
            OpenPopUp();
        }

        private void OpenPopUp()
        {
            var valores = this.Datasource.Parent.ParentImprenta.CalculaValoresInsertos();
            var numero = 1;
            this.panel.Children.Clear();
            this.panel2.Children.Clear();
            foreach (var insertoInfo in valores)
            {
                this.panel.Children.Add(GetPanelInformaciob("No. Inserto: ", numero.ToString()));
                this.panel.Children.Add(GetPanelInformaciob("Páginas: ", insertoInfo.Paginas.ToString()));
                this.panel.Children.Add(GetPanelInformaciob("Pliegos por inserto: ", insertoInfo.PliegosPorElemento.ToString()));
                this.panel.Children.Add(GetPanelInformaciob("Pliegos por tiraje: ", insertoInfo.GetLeyendaPliegos()));
                this.panel.Children.Add(GetPanelInformaciob("Papel: ", insertoInfo.Papel));
                this.panel.Children.Add(GetPanelInformaciob("Gramaje: ", insertoInfo.GetGramos()));
                this.panel.Children.Add(GetPanelInformaciob("Tintas: ", insertoInfo.Tintas));
                this.panel.Children.Add(GetPanelInformaciob(string.Empty, insertoInfo.SujetoSuelto));
                this.panel.Children.Add(GetPanelInformaciob("Posicion: ", insertoInfo.Posicion, true));

                this.panel2.Children.Add(GetPanelInformaciob("No. Inserto: ", numero.ToString()));
                this.panel2.Children.Add(GetPanelInformaciob("Papel: ", insertoInfo.Papel));
                this.panel2.Children.Add(GetPanelInformaciob("Tamaño:", insertoInfo.Medida.ToString()));
                this.panel2.Children.Add(GetPanelInformaciob("Pliegos por tiraje: ",
                                                             insertoInfo.TotalPliegosConMargen.ToString()));
                this.panel2.Children.Add(GetPanelInformaciob("Tintas:", insertoInfo.Tintas));
                this.panel2.Children.Add(GetPanelInformaciob("Costo por pliego: $",
                                                             insertoInfo.CostoPliegoindividual.ToString("N2")));
                this.panel2.Children.Add(GetPanelInformaciob("Costo de papel: $", insertoInfo.CostoLotePliegos.ToString("N2"), true));
                this.panel2.Children.Add(GetPanelInformaciob("Costo impresión : ", insertoInfo.CostoImpresionMargenTintas.ToString("N2")));              
                this.panel2.Children.Add(GetPanelInformaciob("Costo placas: ", "$" + insertoInfo.GetCostoPlacasTintas().ToString("N2")));
                this.panel2.Children.Add(GetPanelInformaciob("Costo total : ", "$" + insertoInfo.GetCostoTotal().ToString("N2")));
                numero++;
            }

            Popup1.IsOpen = true;
        }

        private StackPanel GetPanelInformaciob(string titulo, string contenido,bool marginButtom=false)
        {
            var chPanel = new StackPanel { Orientation = Orientation.Horizontal, };
            var lblTitulo = new Label() { Content = titulo, FontWeight = FontWeights.Bold };
            var lblContenido = new Label() { Content = contenido };
            if (marginButtom)
            {
                chPanel.Margin = new Thickness(0,0,0,20);
            }
            chPanel.Children.Add(lblTitulo);
            chPanel.Children.Add(lblContenido);
            return chPanel;
        }

        private void ComprarPor_OnCalculoEvent(object sender, EventArgs e)
        {
            OpenPopUp();
        }

        private void BtnUp_OnClick(object sender, RoutedEventArgs e)
        {
            double insertos;
            var esnumero = double.TryParse(this.txtNoInsertos.Text, out insertos);
            this.txtNoInsertos.Text = esnumero ? (insertos + 1).ToString() : "0";
        }

        private void Btndown_OnClick(object sender, RoutedEventArgs e)
        {
            double insertos;
            var esnumero = double.TryParse(this.txtNoInsertos.Text, out insertos);
            this.txtNoInsertos.Text = esnumero && insertos >0? (insertos - 1).ToString() : "0";
        }
    }
}
